import pygame

# 棋盘的位置数据
POSITION = { 0:0, 1:1, 2:2, 3:3, 4:4, 5:5, 6:6, 7:7, 8:8, 9:9 }
ADJACENT = {
        0: [5, 7],
        1: [6, 8],
        2: [7, 9],
        3: [5, 8],
        4: [6, 9],
        5: [0, 3],
        6: [1, 4],
        7: [0, 2],
        8: [1, 3],
        9: [2, 4],
    }

def check_first():
    for i in range(10):
        if POSITION[i] == -1:
            return False
    return True

def modify_piecesstatus( pos ):
    if check_first():
        POSITION[pos] = -1
    else:
        POSITION[pos] = -1

def getposiempty( pos ):
    return POSITION[pos] != -1

def calculate_intersection(segment1, segment2):
    # 线段1的起点和终点
    P1, P2 = segment1[0], segment1[1]
    # 线段2的起点和终点
    P3, P4 = segment2[0], segment2[1]

    # 计算斜率
    m1 = (P2[1] - P1[1]) / (P2[0] - P1[0]) if (P2[0] - P1[0]) != 0 else None
    m2 = (P4[1] - P3[1]) / (P4[0] - P3[0]) if (P4[0] - P3[0]) != 0 else None

    # 判断线段关系
    if m1 is None and m2 is None:
        # 两条线段都是垂直线段且平行，无交点
        return None
    elif m1 is None:
        # 线段1是垂直线段，线段2不是
        intersection_x = P1[0]
        intersection_y = m2 * (intersection_x - P3[0]) + P3[1]
        return (intersection_x, intersection_y)
    elif m2 is None:
        # 线段2是垂直线段，线段1不是
        intersection_x = P3[0]
        intersection_y = m1 * (intersection_x - P1[0]) + P1[1]
        return (intersection_x, intersection_y)
    elif m1 == m2:
        # 两条线段平行，无交点
        return None
    else:
        # 计算交点的 x 坐标
        intersection_x = (m1 * P1[0] - m2 * P3[0] - P1[1] + P3[1]) / (m1 - m2)
        # 计算交点的 y 坐标
        intersection_y = m1 * (intersection_x - P1[0]) + P1[1]
        return (intersection_x, intersection_y)

